#include "StdAfx.h"
#include "PitchTracker.h"
#include "Sample.h"
#include "WaveletPitchProcessor.h"

PitchTracker::PitchTracker(const	unsigned	int	bufferSize,
							const	unsigned	int	samplingFrequency)
{
	processor = new WaveletPitchProcessor(bufferSize, samplingFrequency);
	lastPitch = 120;
}

PitchTracker::~PitchTracker(void)
{
	delete processor;
}

unsigned int PitchTracker::GetFrequency(short* sample, unsigned int length, std::vector<SampleMarker>* markers)
{
	double amplitudeThreshold = 0.85;
	double noiseThreshold = 6.0;

	Sample cur(sample, length);

	PitchOut out;
	Sample lpSample;
	cur.LowPass400Filter(lpSample);
	processor->trackPitch(lpSample.GetData(), amplitudeThreshold, noiseThreshold, &out);
	if(!out.voiced || out.pitch<1)
	{
		return 0;
	}
	int frequency = (int)(out.pitch * 2);

	if(frequency<65) frequency = 0;
	else if(frequency>300) frequency = 0;
	else
	{		
		lastPitch = frequency;
	}
	return frequency;
}